********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*****
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global y "lncdisbur_w199"
global regcommands ", pool(5) absorb(bankid borrowerid#time) vce(cluster bankid)"
global regcommands2 ", pool(5) absorb(bankid#time borrowerid#time) vce(cluster bankid)"

**#Bookmark (input)
clear *
use $inputdirectory/INB_preann2019m8
sum ext1r, d
scalar define s1=r(p25)

**#Bookmark (input)
import sas using $inputdirectory/bloansn2019, case(lower) clear
rename utilizzato disbur
rename accordato com
rename data_oss time

keep if disbur>=30000|com>=30000
drop com
rename ente_segn bankid
rename cod_cen borrowerid
format borrowerid %20.0g
format disbur %20.0g

gen xxx=date(time, "DMY")
drop time
gen time=mofd(xxx)
drop xxx
format time %tm

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e14 exlnassets excashas ext1r)
keep if _merge==3
drop _merge

egen id=group(bankid borrowerid)

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2019m3)&time<=tm(2020m2)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)
replace ext1r=ext1r-scalar(s1)

run RO_tdummies 2019 1
run RO_tdintera 1 "excashas ext1r exlnassets e14"

gen dT7e14ext1r=dT7e14*ext1r

**#Bookmark (input)
merge m:1 borrowerid using $inputdirectory/INB_cerveds2019
keep if _merge!=2

foreach x of varlist sbig score42 {
gen e14`x'=e14*`x'
gen dT7e14`x'=dT7e14*`x'
gen e14`x'ext1r=e14*`x'*ext1r
gen dT7`x'ext1r=dT7ext1r*`x'
gen dT7e14`x'ext1r=dT7e14*`x'*ext1r
}
gen score42ext1r= score42*ext1r

**#Bookmark (output - columns 6-1-2-3-4-5)
est clear
cd $outputdirectory
reghdfe $y dT7e14score42 dT7e14score42ext1r e14score42 score42ext1r dT7score42ext1r e14score42ext1r if score42!=. $regcommands2
gen sample=e(sample)
est save TAB8A_C612345
reghdfe $y dT7e14 dT7excashas dT7exlnassets lncrefin if sample==1 $regcommands
est save TAB8A_C612345, append
reghdfe $y dT7e14 dT7e14score42 e14score42 dT7excashas dT7exlnassets lncrefin if sample==1 $regcommands
est save TAB8A_C612345, append
reghdfe $y dT7e14 dT7e14sbig dT7e14score42 e14score42 e14sbig dT7excashas dT7exlnassets lncrefin if sample==1 $regcommands
est save TAB8A_C612345, append
reghdfe $y dT7e14 dT7e14score42 dT7e14score42ext1r dT7e14ext1r dT7score42ext1r dT7ext1r e14score42ext1r e14score42 score42ext1r dT7excashas dT7exlnassets lncrefin if sample==1 $regcommands
est save TAB8A_C612345, append
reghdfe $y dT7e14sbig dT7e14score42 e14score42 e14sbig if sample==1 $regcommands2
est save TAB8A_C612345, append
cd ..

********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global y "lncdisbur_w199"
global regcommands ", pool(5) absorb(bankid borrowerid#time) vce(cluster bankid)"
global regcommands2 ", pool(5) absorb(bankid#time borrowerid#time) vce(cluster bankid)"

**#Bookmark (input)
clear *
use $inputdirectory/INB_preann2014m12
sum ext1r, d
scalar define s1=r(p25)

**#Bookmark (input)
import sas using $inputdirectory/bloansn2015total, case(lower) clear
rename utilizzato disbur
rename accordato com
rename data_oss time

keep if disbur>=30000|com>=30000
drop com
rename ente_segn bankid
rename cod_cen borrowerid
format borrowerid %20.0g
format disbur %20.0g

gen xxx=date(time, "DMY")
drop time
gen time=mofd(xxx)
drop xxx
format time %tm

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2015, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e13 exlnassets excashas ext1r)
keep if _merge==3
drop _merge
egen id=group(bankid borrowerid)

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2014m7)&time<=tm(2015m6)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)
replace ext1r=ext1r-scalar(s1)

run RO_tdummies 2015 1
run RO_tdintera 1 "excashas ext1r exlnassets e13"

gen dT7e13ext1r=dT7e13*ext1r

**#Bookmark (input)
merge m:1 borrowerid using $inputdirectory/INB_cerveds2015
keep if _merge!=2

foreach x of varlist sbig score42 {
gen e13`x'=e13*`x'
gen dT7e13`x'=dT7e13*`x'
gen e13`x'ext1r=e13*`x'*ext1r
gen dT7`x'ext1r=dT7ext1r*`x'
gen dT7e13`x'ext1r=dT7e13*`x'*ext1r
}
gen score42ext1r= score42*ext1r

**#Bookmark (output - columns 6-1-2-3-4-5)
est clear
cd $outputdirectory
reghdfe $y dT7e13score42 dT7e13score42ext1r e13score42 score42ext1r dT7score42ext1r e13score42ext1r if score42!=. $regcommands2
gen sample=e(sample)
est save TAB8B_C612345
reghdfe $y dT7e13 dT7excashas dT7exlnassets lncrefin if sample==1 $regcommands
est save TAB8B_C612345, append
reghdfe $y dT7e13 dT7e13score42 e13score42 dT7excashas dT7exlnassets lncrefin if sample==1 $regcommands
est save TAB8B_C612345, append
reghdfe $y dT7e13 dT7e13sbig dT7e13score42 e13score42 e13sbig dT7excashas dT7exlnassets lncrefin if sample==1 $regcommands
est save TAB8B_C612345, append
reghdfe $y dT7e13 dT7e13score42 dT7e13score42ext1r dT7e13ext1r dT7score42ext1r dT7ext1r e13score42ext1r e13score42 score42ext1r dT7excashas dT7exlnassets lncrefin if sample==1 $regcommands
est save TAB8B_C612345, append
reghdfe $y dT7e13sbig dT7e13score42 e13score42 e13sbig if sample==1 $regcommands2
est save TAB8B_C612345, append
cd ..


